/**
 *    Copyright (C) 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_MODELS_COLLECTION_MODEL_MODEL_API_HPP
#define _CUBEFS_CORE_MODELS_COLLECTION_MODEL_MODEL_API_HPP

#include <cubefs/core/models/collection/names.hpp>
#include <cubefs/api/models/logs.hpp>

#define CUBEFS_SOURCE "cubefs/core/models/collection/model/model_api.hpp:" CUBEFS_AT

namespace cubefs    {
namespace ctr       {

CUBEFS_MODEL_PART_BEGIN(cubefs::models::collection::ModelApiName)

    typedef typename Base::Types                                                Types;
    typedef typename Base::PageMgr                                              PageMgr;

    typedef typename Base::Page                                                 Page;
    typedef typename Base::ID                                                   ID;


    typedef typename Base::NodeBase                                             NodeBase;
    typedef typename Base::Counters                                             Counters;
    typedef typename Base::Iterator                                             Iterator;

    typedef typename Base::NodeDispatcher                                       NodeDispatcher;
    typedef typename Base::RootDispatcher                                       RootDispatcher;
    typedef typename Base::LeafDispatcher                                       LeafDispatcher;
    typedef typename Base::NonLeafDispatcher                                    NonLeafDispatcher;

    typedef typename Base::Node2RootMap                                         Node2RootMap;
    typedef typename Base::Root2NodeMap                                         Root2NodeMap;

    typedef typename Base::NodeFactory                                          NodeFactory;

    typedef typename Base::Key                                                  Key;
    typedef typename Base::Value                                                Value;

    typedef typename Base::ApiKeyType                                           ApiKeyType;
    typedef typename Base::ApiValueType                                         ApiValueType;

    static const Int Indexes                                                    = Base::Indexes;


    virtual Typed* CreateElementHolder() {
        return NULL;
    }

    virtual cubefs::api::Iterator *CreateIterator() {
        return new Iterator(me_.FindStart());
    }

    virtual BigInt GetSize() {
        return me_.GetTotalKeyCount();
    }

    virtual bool Add(Typed *holder) {
        throw CubefsException(CUBEFS_SOURCE, "Operation is not implemented for this model");

        

//        if (holder == NULL) {
//            throw NullPointerException(CUBEFS_SOURCE, "value holder can't be null");
//        }
//
//        if (holder->type() != Map::Pair::MyType()) {
//            throw TypeCastException(CUBEFS_SOURCE, "value holder is not a Map::Pair", holder->type(), Map::Pair::MyType());
//        }
//
//        Map::Pair* pair = static_cast<Map::Pair*>(holder);
//
//        me_.Put(pair->key(), pair->value(), 0);
    }

//    typedef cubefs::api::Map::Pair Pair;

    

//    virtual cubefs::api::Iterator* Find(Typed *value, Int i, Int type, bool for_insert) {
//        ApiKeyType* key = TypedCast<ApiKeyType>(value);
//        Iterator iter = type == cubefs::api::TreeMap::LE ? me_.FindLE(key->getValue(), i, for_insert) : me_.FindLT(key->getValue(), i, for_insert);
//        return new Iterator(iter);
//    }

    virtual bool Remove(cubefs::api::Iterator *iterator) {
        throw CubefsException(CUBEFS_SOURCE, "Operation is not implemented for this model");

//        Iterator* i = static_cast<Iterator*>(iterator);
//        if (&i->model() == &me_) {
//            return me_.RemoveEntry(*i);
//        }
//        else {
//            throw CubefsException(CUBEFS_SOURCE, "Invalid model");
//        }
    }

//    virtual cubefs::api::Iterator* Find(Typed *key) {
//        return Find(key, 0, cubefs::api::TreeMap::LE, false);
//    }

//    virtual bool Remove(cubefs::api::Iterator *from, cubefs::api::Iterator *to) {
//        if (from == NULL) throw NullPointerException(CUBEFS_SOURCE, "from must not be null");
//        if (to == NULL) throw NullPointerException(CUBEFS_SOURCE, "to must not be null");
//
//        Iterator* ifrom = static_cast<Iterator*>(from);
//        Iterator* ito = static_cast<Iterator*>(to);
//
//        if (&ifrom->model() != &me_)    throw CubefsException(CUBEFS_SOURCE, "Invalid model for <from> iterator");
//        if (&ito->model() != &me_)      throw CubefsException(CUBEFS_SOURCE, "Invalid model for <to> iterator");
//
//        return me_.RemoveEntries(*ifrom, *ito);
//    }

CUBEFS_MODEL_PART_END

}
}

#undef CUBEFS_SOURCE

#endif
